let path = require('path');

// let HtmlWebpackPlugin = require('html-webpack-plugin');
let CopyWebpackPlugin = require('copy-webpack-plugin');
let webpack = require('webpack');

module.exports = {
  mode : 'development', // development | production
  entry : {
    'scene/display' : './src/index.js'
  },
  output : {
    filename : '[name].js',
    chunkFilename: '_bundle/[name].bundle.js',
    path : path.resolve(__dirname, 'dist'),
    publicPath: "/"
  },

  devtool : 'cheap-module-source-map',

  // server
  devServer : {
    port : 3000,
    contentBase : 'dist',
    before : function (app) {
      // app.get('/test', (request, response) => {
      //   response.json({result : true, message : 'test 成功'});
      // });
      app.get('/aurora/asset/allSeries', (request, response) => {
        response.redirect('/aurora/asset/allSeries.json');
      });
    }
  },

  plugins : [
    // new HtmlWebpackPlugin({
    //   template : './public/template/scene.html',
    //   filename : 'test.html',
    //   chunks : ['test']
    // }),
    // new HtmlWebpackPlugin({
    //   template : './public/template/snowbros.html',
    //   filename : 'scene/snowbros.html',
    //   chunks : ['scene/display']
    // }),
    new CopyWebpackPlugin([
      {
        from : path.resolve(__dirname, 'public', 'static'),
        to : path.resolve(__dirname, 'dist')
      },
      {
        from : path.resolve(__dirname, '..', 'aurora-static', 'package'),
        to : path.resolve(__dirname, 'dist', 'aurora-res')
      },
      // comp
      {
        from : path.resolve(__dirname, 'dist-comp', 'aurora-res'),
        to : path.resolve(__dirname, 'dist', 'aurora-res')
      },
    ]),
    new webpack.DefinePlugin({
      __author__ : JSON.stringify('Zdream'),
      __websocket_enable__ : JSON.stringify(true)
    }),
    // antd 引用了 moment. 这个插件告诉它, 我不需要 moment 的 locale （本地化）文件, 英文就行
    // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
    // 只用中文的话 : new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /zh-cn/)
    new webpack.IgnorePlugin(/\.\/locale$/, /moment$/),
    // new webpack.DllReferencePlugin({
    //   // manifest : path.resolve(__dirname, 'dist', '_lib', 'manifest.json')
    //   name: '_dll_antd',
    //   manifest : path.resolve(__dirname, 'dist', '_lib', 'manifest_ants.json'),
    //   context: path.resolve(__dirname),
    // })
  ],

  resolve : {
    modules : [path.resolve('node_modules')],
    extensions : ['.js', '.css', '.json']
  },

  module : {
    rules : [
      {
        test : /\.(png|jpg|gif)$/,
        use : 'file-loader'
      },
      {
        test : /\.js$/,
        exclude : /node_modules/,
        use : [
          {
            loader : 'babel-loader',
            options : {
              presets : [
                '@babel/preset-env'/*, '@babel/preset-react'*/
              ],
              plugins : [
                '@babel/plugin-proposal-class-properties',
              ]
            }
          }
        ]
      },
      {
        test : /\.css$/,
        use : ['style-loader', 'css-loader']
      },
      {
        test : /\.less$/,
        use : 
        [
          'style-loader',
          'css-loader',
          {
            loader : 'less-loader',
            options : {
              javascriptEnabled: true
            }
          }
        ]
      }
    ]
  },

  // optimization: {
  //   splitChunks: {
  //     chunks: 'all',
  //   },
  // }
}